<template>
  <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
    <div class="home">
      <movie-item v-for="(item,index) of movies" :key="index" :movie="item" @jump="onJump"></movie-item>
    </div>
  </van-list>
</template>

<script>
import MovieItem from "../components/MovieItem";
import axios from "axios-jsonp-pro";
export default {
  name: "home",
  data() {
    return {
      movies: [],
      loading: false,
      finished: false,
      total: ""
    };
  },
  components: {
    MovieItem
  },
  mounted() {
    // var url = "https://douban.uieee.com/v2/movie/top250";
    // axios.jsonp(url).then(res => {
    //   this.handleData(res);
    // });
  },
  methods: {
    handleData(res) {
      var subjects = res.subjects;
      var movies = [];
      subjects.forEach(item => {
        var temp = {
          id: item.id,
          imageUrl: item.images.small,
          title: item.title
        };
        movies.push(temp);
      });
      this.total = res.total;
      this.movies = [...this.movies,...movies];
    },
    onLoad() {
      setTimeout(() => {
        var length = this.movies.length;
        var url = `https://douban.uieee.com/v2/movie/coming_soon?start=${length}&count=20`;
        axios.jsonp(url).then(res => {
          this.handleData(res);
          this.loading = false;
          if(length >= this.total){
            this.finished = true;
          }
        });
      }, 500);
    },
    onJump(id) {
      this.$router.push("/about/" + id);
    },
    toggle() {
      this.isShow = !this.isShow;
    }
  }
};
</script>
<style lang="scss" scoped>
.home {
  max-width: 901px;
  display: flex;
  flex-wrap: wrap;
  margin-left: auto;
  margin-right: auto;
}
.v-enter,
.v-leave-to {
  opacity: 0;
}
.v-enter-active,
.v-leave-active {
  transition: opacity 6s;
}
</style>
